package sk.robocup.dvanastyhrac.config;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import sk.robocup.dvanastyhrac.run.Run12hrac;
import sk.robocup.dvanastyhrac.tactics.formation.PlayerFormationInfo12.FORMATION;

/**
 * 
 * Trieda zabezpecuje nacitavanie meno timu adresy a hracov zo suboru. Trieda je
 * singleton cize v aplikacii sa pouziva iba jedna instancia
 * 
 * @author ligocky
 * 
 */
public class ConfigUtil {

    /**
     * tieto hodnoty budu defaltne ked sa to nepodari nacitat zo suboru
     */
    private static String TEAM_NAME = "12Hrac5";
    private static String HOST = "127.0.0.1";
    private static boolean CONNECT_GUI = true;
    private static boolean RECORD = true;
    private static String STRATEGY_ATTACK = FORMATION.ATTACK1.name();
    private static String STRATEGY_DEFENSE = FORMATION.DEFENSE1.name();
    private static String PLAYER_1 = "goalie.conf";
    private static String PLAYER_2 = "defender.conf";
    private static String PLAYER_3 = "defender.conf";
    private static String PLAYER_4 = "defender.conf";
    private static String PLAYER_5 = "defender.conf";
    private static String PLAYER_6 = "player.conf";
    private static String PLAYER_7 = "player.conf";
    private static String PLAYER_8 = "player.conf";
    private static String PLAYER_9 = "player.conf";
    private static String PLAYER_10 = "player.conf";
    private static String PLAYER_11 = "player.conf";
    private static ConfigUtil config = null;
    private static String CONFIG_FILE_NAME = "config.txt";

    public static ConfigUtil getConfig() {
        return config;
    }

    public String getCONFIG_FILE_NAME() {
        return CONFIG_FILE_NAME;
    }

    private ConfigUtil() {
        this(CONFIG_FILE_NAME);
    }

    private ConfigUtil(String fileName) {
        CONFIG_FILE_NAME = fileName;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException e) {
            try {
                InputStream stream = Run12hrac.class.getClassLoader().getResourceAsStream(fileName);
                in = new BufferedReader(new InputStreamReader(stream));
            } catch (Exception e1) {
                e1.printStackTrace();
                System.err.println("Dany subor sa nenasiel: " + fileName);
            }
        }
        if (in == null) {
            System.err.println("Dany subor sa nenasiel: " + fileName);
            return;
        }
        String str;
        try {
            while ((str = in.readLine()) != null) {
                if (str.startsWith("//")) {
                    continue;
                }
                String[] riadok = str.split(":");
                if (riadok.length == 1) {
                    continue;
                }
                if (riadok[0].equals("Teamname")) {
                    TEAM_NAME = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Host")) {
                    HOST = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Connectgui")) {
                    if (riadok[1].equals("true")) {
                        CONNECT_GUI = true;
                    } else {
                        CONNECT_GUI = false;
                    }
                    continue;
                }
                if (riadok[0].equals("Record")) {
                    if (riadok[1].equals("true")) {
                        RECORD = true;
                    } else {
                        RECORD = false;
                    }
                    continue;
                }
                if (riadok[0].equals("Hrac1")) {
                    PLAYER_1 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac2")) {
                    PLAYER_2 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac3")) {
                    PLAYER_3 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac4")) {
                    PLAYER_4 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac5")) {
                    PLAYER_5 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac6")) {
                    PLAYER_6 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac7")) {
                    PLAYER_7 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac8")) {
                    PLAYER_8 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac9")) {
                    PLAYER_9 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac10")) {
                    PLAYER_10 = riadok[1];
                    continue;
                }
                if (riadok[0].equals("Hrac11")) {
                    PLAYER_11 = riadok[1];
                }
                if (riadok[0].equals("StrategyAttack")) {
                    STRATEGY_ATTACK = riadok[1];
                }
                if (riadok[0].equals("StrategyDefense")) {
                    STRATEGY_DEFENSE = riadok[1];
                }
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            System.err.println("Chyba pri nacitavani zo suboru");
        }
    }

    public void refreshStrategy() {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(CONFIG_FILE_NAME));
        } catch (FileNotFoundException e) {
            System.err.println("Dany subor sa nenasiel" + CONFIG_FILE_NAME);
        }
        String str;
        try {
            while ((str = in.readLine()) != null) {
                if (str.startsWith("//")) {
                    continue;
                }
                String[] riadok = str.split(":");
                if (riadok.length == 1) {
                    continue;
                }
                if (riadok[0].equals("StrategyAttack")) {
                    STRATEGY_ATTACK = riadok[1];
                }
                if (riadok[0].equals("StrategyDefense")) {
                    STRATEGY_DEFENSE = riadok[1];
                }
            }
        } catch (IOException e) {
            System.err.println("Chyba pri nacitavani zo suboru");
        }

    }

    public static ConfigUtil get() {
        if (config == null) {
            config = new ConfigUtil();
        }
        return config;
    }

    public static ConfigUtil get(String fileName) {
        if (config == null) {
            config = new ConfigUtil(fileName);
        }
        return config;
    }

    public String getTEAM_NAME() {
        return TEAM_NAME;
    }

    public String getHOST() {
        return HOST;
    }

    public boolean isCONNECT_GUI() {
        return CONNECT_GUI;
    }

    public boolean isRECORD() {
        return RECORD;
    }

    public String getPLAYER_1() {
        return PLAYER_1;
    }

    public String getPLAYER_2() {
        return PLAYER_2;
    }

    public String getPLAYER_3() {
        return PLAYER_3;
    }

    public String getPLAYER_4() {
        return PLAYER_4;
    }

    public String getPLAYER_5() {
        return PLAYER_5;
    }

    public String getPLAYER_6() {
        return PLAYER_6;
    }

    public String getPLAYER_7() {
        return PLAYER_7;
    }

    public String getPLAYER_8() {
        return PLAYER_8;
    }

    public String getPLAYER_9() {
        return PLAYER_9;
    }

    public String getPLAYER_10() {
        return PLAYER_10;
    }

    public String getPLAYER_11() {
        return PLAYER_11;
    }

    public String getSTRATEGY_ATTACK() {
        return STRATEGY_ATTACK;
    }

    public String getSTRATEGY_DEFENSE() {
        return STRATEGY_DEFENSE;
    }
}
